const { Admin } = require("../../../Models");
const { ERR, E } = require('../../../../../constants')
const { CryptUtil } = require('../../../Util')
const Hash = use('Hash')
class AdminController {
  async login({ request, response, params }) {
    let { name, password } = request.all()
    name = decodeURI(name)
    let vo = await Admin.query().where('name', name).first()
    if (!vo) {
      response.json({
        token: 0,
        name
      })
    } else {
      const isSame = await Hash.verify(password, vo.password)
      vo = vo.toJSON()
      delete vo.password
      if (isSame) {
        response.json({
          token: CryptUtil.jwtEncode({ adminID: vo.id }),
          name,
          admin: vo
        })
      } else {
        response.json({
          token: 0,
          name
        })
      }
    }
  }
  async getUserInfo({ request, response, params }) {
    let user = request.adminUser
    response.json({ error: 0, user })
  }
}

module.exports = AdminController